﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace PersonalAttendanceLojaSoft
{
    public class AccesoDatosSQL
    {

        public string NombreTabla = "RegistroPersonal";

        public string CadenaConexion()
        {
            SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
            csb.DataSource = @"(local)\SQLEXPRESS";
            csb.InitialCatalog = "PersonalAttendance";
            csb.IntegratedSecurity = true;
            return csb.ConnectionString;
        }

        public string Insertar(string cedula, DateTime fecha, string descripcion)
        {
            string sCon = CadenaConexion();
            string sel;

            sel = "INSERT INTO " + NombreTabla + "(Cedula, Fecha, Descripcion) " + "VALUES " + "(@Cedula, @Fecha, @Descripcion) " + "SELECT @@Identity";

            using (SqlConnection con = new SqlConnection(sCon))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand(sel, con);
                    cmd.Parameters.AddWithValue("@Cedula", cedula);
                    cmd.Parameters.AddWithValue("@Fecha", fecha);
                    cmd.Parameters.AddWithValue("@Descripcion", descripcion);
                    con.Open();
                    cmd.ExecuteScalar();
                    con.Close();
                    return "Registro Correcto";
                }
                catch (SqlException ex)
                {
                    return Convert.ToString(ex);
                }
            }
        }
    }
}
